/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sdd.dispatchers;

import com.sdd.facades.view.ProductViewFacade;
import com.sdd.plains.ProductPlain;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ceu
 */
@WebServlet(name = "ProductDispatcher", urlPatterns = {"/Prod"})
public class ProductDispatcher extends HttpServlet {

     @EJB
    ProductViewFacade productViewFacade;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String prodId = request.getParameter("id");
            out.println("<html><body>");
            if (prodId != null && !prodId.equalsIgnoreCase("")) {
                Long id = new Long(prodId);
                ProductPlain requested = productViewFacade.getProduct(id);
                out.print("The requested product was: " + requested + "<br>");
            }


            out.println("<form action='' method='post'>");
            out.println("<p>Product name<input type='text' name='prodname'/></p>");
            out.println("<p>Product price<input type='text' name='prodprice'/></p>");
            out.println("<p>Product description<input type='text' name='proddesc'/></p>");
            //out.println("<p>Category (father) ID: <input type='text' name='prodfather'/></p>");
            out.println("<p><input type='submit' value='create'/></p>");
            out.println("</form>");
            out.println("</body></html>");
        } finally {
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ProductPlain p = new ProductPlain();
        String name = request.getParameter("prodname");
        String price = request.getParameter("prodprice");
        String desc = request.getParameter("proddesc");
        ProductPlain returned = null;
        p.setName(name);
        p.setPrice(Double.parseDouble(price));
        p.setDescription(desc);
        returned = productViewFacade.addProduct(p);
        response.sendRedirect("/SDDStore-war/Prod?id=" + returned.getProductId());

    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
